package com.xzr.kutumanager.controller;

import com.xzr.kutumanager.entity.UserInfo;
import com.xzr.kutumanager.service.UserService;
import com.xzr.kutumanager.util.ConstVariable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;


/**
 * @Author：徐志荣
 * @Date：2023/7/18 下午 4:49
 */

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public boolean login(HttpServletRequest request, String loginname, String password) {

        if (StringUtils.hasLength(loginname) && StringUtils.hasLength(password)) {
            //参数有效
            UserInfo userInfo = userService.login(loginname, password);
            if (userInfo != null && userInfo.getUid() > 0) {
                //存储session
                HttpSession session = request.getSession(true);
                session.setAttribute(ConstVariable.USER_SESSION_KEY, userInfo);

                return true;
            }

        }
        return false;
    }

    //查询所有列表 无分页
    @RequestMapping("/list")
    public List<UserInfo> getAll() {
        return userService.getAll();
    }

}
